﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CE.iPhone.PList;
namespace cczEdit.DramaDialog
{
    public partial class EditDramaSetWinReward : Form
    {
        PListDict curDict;
        public EditDramaSetWinReward()
        {
            InitializeComponent();
        }
        public void Show(IWin32Window owner, PListDict dict)
        {
            curDict = dict;
            listBoxItems.Items.Clear();
            PListInteger money = (PListInteger)curDict["Money"];
            PListDict items = (PListDict)curDict["ItemDic"];
            foreach (KeyValuePair<string, IPListElement> kv in items)
            {
                listBoxItems.Items.Add(kv.Key);
               //PListDict itemDict = (PListDict)kv.Value;

            }
            textBoxMoney.Text = money.Value.ToString();

            if (listBoxItems.Items.Count <= 0)
                buttonDel.Enabled = false;
            else
            {
                buttonDel.Enabled = true;
                listBoxItems.SelectedIndex = 0;
            }
            ShowDialog(owner);
        }
        private void comboBoxItemType_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBoxItemID.Items.Clear();
            if (comboBoxItemType.SelectedItem.ToString() == "Weapon")
            {
                foreach (Equipment equip in UCDrama.weapons)
                {
                    comboBoxItemID.Items.Add(equip.name);
                }
            }
            else if (comboBoxItemType.SelectedItem.ToString() == "Armor")
            {
                foreach (Equipment equip in UCDrama.armors)
                {
                    comboBoxItemID.Items.Add(equip.name);
                }
            }else if (comboBoxItemType.SelectedItem.ToString() == "Assist")
            {
                foreach (Equipment equip in UCDrama.assists)
                {
                    comboBoxItemID.Items.Add(equip.name);
                }
            }
            else if (comboBoxItemType.SelectedItem.ToString() == "Sundry")
            {
                foreach (Sundry equip in UCDrama.sundries)
                {
                    comboBoxItemID.Items.Add(equip.name);
                }
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (comboBoxItemID.SelectedItem == null || comboBoxItemType.SelectedItem == null)
                return;

            PListDict items = (PListDict)curDict["ItemDic"];
            if (items.ContainsKey(comboBoxItemID.SelectedItem.ToString()))
                return;
            PListDict item = new PListDict();
            items.Add(comboBoxItemID.SelectedItem.ToString(), item);
            item.Add("Type", new PListString(comboBoxItemType.SelectedItem.ToString()));
            item.Add("Num", new PListInteger(GetItemID(comboBoxItemType.SelectedItem.ToString(), comboBoxItemID.SelectedItem.ToString())));
            item.Add("Level", new PListInteger(System.Int32.Parse(textBoxItemLevel.Text)));
            listBoxItems.Items.Add(comboBoxItemID.SelectedItem.ToString());
            listBoxItems.SelectedItem = comboBoxItemID.SelectedItem.ToString();
            if (listBoxItems.Items.Count > 1)
                buttonDel.Enabled = true;
        }

        private void listBoxItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxItems.SelectedItem == null)
                return;
            PListDict items = (PListDict)curDict["ItemDic"];
            PListDict item = (PListDict)items[listBoxItems.SelectedItem.ToString()];
            PListString itemType = (PListString)item["Type"];
            PListInteger itemNum = (PListInteger)item["Num"];
            PListInteger itemLevel = (PListInteger)item["Level"];
            textBoxItemLevel.Text = itemLevel.Value.ToString();
            comboBoxItemType.SelectedItem = itemType.Value;
            comboBoxItemID.SelectedItem = GetItemName(comboBoxItemType.SelectedItem.ToString(),(int)itemNum.Value);
            
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            PListDict items = (PListDict)curDict["ItemDic"];
            string itemDel = listBoxItems.SelectedItem.ToString();
            PListDict item = (PListDict)items[itemDel];
            item["Type"] = new PListString(comboBoxItemType.SelectedItem.ToString());
            item["Num"] = new PListInteger(GetItemID(comboBoxItemType.SelectedItem.ToString(), comboBoxItemID.SelectedItem.ToString()));
            item["Level"] = new PListInteger(System.Int32.Parse(textBoxItemLevel.Text));
            items.Remove(itemDel);
            items.Add(comboBoxItemID.SelectedItem.ToString(), item);
            listBoxItems.Items.Remove(itemDel);
            listBoxItems.Items.Add(comboBoxItemID.SelectedItem.ToString());
            listBoxItems.SelectedItem = comboBoxItemID.SelectedItem.ToString();
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            if (listBoxItems.Items.Count <= 0)
                return;
            string itemDel = listBoxItems.SelectedItem.ToString();
            listBoxItems.SelectedIndex = 0;
            listBoxItems.Items.Remove(itemDel);
            PListDict items = (PListDict)curDict["ItemDic"];
            items.Remove(itemDel);
            if (listBoxItems.Items.Count <= 1)
                buttonDel.Enabled = false;
        }

        private void buttonAccept_Click(object sender, EventArgs e)
        {
            curDict["Money"] = new PListInteger(System.Int32.Parse(textBoxMoney.Text));
            Hide();

        }
        private long GetItemID(string type, string name)
        {
            if (type == "Weapon")
            {
                Equipment equip = UCDrama.weapons.Find((w) => { return w.name == name; });
                return equip.num;
            }
            else if (type == "Armor")
            {
                Equipment equip = UCDrama.armors.Find((a) => { return a.name == name; });
                return equip.num;
            }
            else if (type == "Assist")
            {
                Equipment equip = UCDrama.assists.Find((a) => { return a.name == name; });
                return equip.num;
            }
            else if (type == "Sundry")
            {
                Sundry equip = UCDrama.sundries.Find((a) => { return a.name == name; });
                return equip.num;
            }
            else
                return -1;
        }
        private string GetItemName(string type, int ID)
        {
            if (type == "Weapon")
            {
                Equipment equip = UCDrama.weapons.Find((w) => { return w.num == ID; });
                return equip.name;
            }
            else if (type == "Armor")
            {
                Equipment equip = UCDrama.armors.Find((a) => { return a.num == ID; });
                return equip.name;
            }
            else if (type == "Assist")
            {
                Equipment equip = UCDrama.assists.Find((a) => { return a.num == ID; });
                return equip.name;
            }
            else if (type == "Sundry")
            {
                Sundry equip = UCDrama.sundries.Find((a) => { return a.num == ID; });
                return equip.name;
            }
            else
                return null;
        }
    }
}
